package pixelwaks.gui.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JPanel;

import common.gui.IViewable;
import common.util.FileHandler;
import common.util.MyClassLoader;

import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;
import pixelwaks.ctrl.ControlFactory;
import pixelwaks.ctrl.impl.ModeCtrl;

/**
 * Class for creating a player information panel, which holds the life/score bar (depending 
 * on the selected mode) and weapon selection view of one Player. The background image 
 * changes to indicate whether or not the Player is the currently active Player.
 * @author Joakim
 *
 */
public class PlayerInfoPanel extends JPanel implements IViewable {

	private static final long serialVersionUID = 1L;
	private Player player;
	private LifeBar lifeBar;
	private PlayerScore playerScore;
	private WeaponSelectView weaponSelectView;
	private BackgroundBorder currentPlayerBorder;
	private BackgroundBorder otherPlayerBorder;
	
	private String status;
	
	/**
	 * Creates a player information panel and adds a LifeBar or PlayerScore and 
	 * a WeaponSelectView.
	 * @param player the Player whose information is to be displayed
	 */
	public PlayerInfoPanel(Player player) {
		this.player = player;
		setLayout(new BorderLayout());
		setBackground(Color.WHITE);
		setVisible(true);
		
		try {
			currentPlayerBorder = new BackgroundBorder(FileHandler.getImage(FileHandler.PLAYER_BACKGROUND, "current_player_bg.png"));
			otherPlayerBorder = new BackgroundBorder(FileHandler.getImage(FileHandler.PLAYER_BACKGROUND, "other_player_bg.png"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(Game.getInstance().getCurrentPlayer() == player)		
			setBorder(currentPlayerBorder);
		else 
			setBorder(otherPlayerBorder);
		
		ModeCtrl mctrl = (ModeCtrl) ControlFactory.getObserver(ControlFactory.MODE_CTRL);
		String res = "resource.modes." + mctrl.getSelectedMode();
		status = ResourceBundle.getBundle(res, Locale.getDefault(), MyClassLoader.getInstance()).getString("status");
		if(status.equals("life")) {
			lifeBar = new LifeBar(player);
			add(lifeBar, BorderLayout.NORTH);
		}
		else if(status.equals("points")) {
			playerScore = new PlayerScore(player);
			add(playerScore, BorderLayout.NORTH);
		}
		
		weaponSelectView = new WeaponSelectView(player);
		add(weaponSelectView, BorderLayout.SOUTH);
		
		Game.getInstance().getMode().addView(this);
	}

	@Override
	public void updateView(Object o) {
		if(Game.getInstance().getCurrentPlayer() == player)		
			setBorder(currentPlayerBorder);
		else 
			setBorder(otherPlayerBorder);
		if(status.equals("life"))
			lifeBar.updateView(null);
		else if(status.equals("points"))
			playerScore.updateView(null);
		weaponSelectView.updateView(null);
	}
}